package com.onenine.app.wxapi;


import android.content.Intent;

import com.onenine.app.R;
import com.onenine.app.activity.PayResultActivity;
import com.onenine.app.event.PaySuccessEvent;
import com.onenine.base.utils.Toasty;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.umeng.share.WxPayMgr;

import org.greenrobot.eventbus.EventBus;

/**
 * 微信支付回调
 */
public class WXPayEntryActivity extends PayResultActivity implements IWXAPIEventHandler {

    @Override
    protected void initViews() {
        super.initViews();
        WxPayMgr.getInstance().handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        WxPayMgr.getInstance().handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
        showLoading(R.string.pay_request_being);
    }

    @Override
    public void onResp(BaseResp resp) {
        dismissLoading();
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            if (resp.errCode == 0) {
                EventBus.getDefault().post(new PaySuccessEvent());
                Toasty.showNormal(R.string.pay_success);
                //todo 从后台查询
            } else if (resp.errCode == -1) {
                Toasty.showNormal(R.string.pay_failure_sign);
                finish();
            } else if (resp.errCode == -2) {
                Toasty.showNormal(R.string.pay_cancel);
                finish();
            } else {
                Toasty.showNormal(R.string.pay_failure);
                finish();
            }
        }
    }

}